<?php
namespace Report\Controller;
use Think\Controller;
class UserController extends Controller {
    public function getChildsClass($data,$uid,$arr,$n){//获取下线层级,用户信息
        $n++;
        foreach ($data as $k => $v){
            if($uid==$v['user_recommend']){
                $v['class']=$n;
                $arr[]=$v;
                unset($data[$k]);
                $arr=$this->getChildsClass($data,$v['user_id'],$arr,$n);
            }
        }
        return $arr;
    }

    public function index(){//获取用户下线
        $account=I('post.account');
        if(!empty($account)){
            $model=M('user');
            $user=$model->field('user.user_id,user.user_name,user.user_recommend,user.group,user.country,user.user_phone,position.user_position')
                ->join('position ON user.position = position.id','left')->where('user.user_id = '.$account)->find();
            $user['class']='';
            $arr[]=$user;
            $users=$model->field('user.user_id,user.user_name,user.user_recommend,user.group,user.country,user.user_phone,position.user_position')
                ->join('position ON user.position = position.id','left')->where('user.user_id > 1000')->select();
            if(empty($users)){
                $this->ajaxReturn(array('status'=>0,'msg'=>'没有找到数据'));
            }else{
                $list=$this->getChildsClass($users,$account,$arr,0);
                $this->ajaxReturn(array('status'=>1,'msg'=>$list,'acc'=>$account));
            }
        }
        $this->display();
    }

    public function downloadData(){//下载客户数据
        $account=I('account') ? I('account') : die('请设置你要查找的账号，<a href="javascript:history.back(-1)">点击返回</a>');
        $model=M('user');
        $user=$model->field('user.user_id,user.user_name,user.user_recommend,user.group,user.country,user.user_phone,position.user_position')
            ->join('position ON user.position = position.id','left')->where('user.user_id = '.$account)->find();
        $user['class']='';
        if(empty($user)){exit('没有找到该账号，<a href="javascript:history.back(-1)">点击返回</a>');}
        $arr[]=$user;
        $users=$model->field('user.user_id,user.user_name,user.user_recommend,user.group,user.country,user.user_phone,position.user_position')
            ->join('position ON user.position = position.id','left')->where('user.user_id > 1000')->select();
        $list=$this->getChildsClass($users,$account,$arr,0);
        $csv=new \Think\Csv();
        $csv_title=array('MT4','姓名','代理人','组别','国家','电话','职位','层级');
        $csv->put_csv($list,$csv_title);
    }

    public function downloadAll(){//倒出所有用户
        $users=M('user')->field('user.user_id,user.user_name,user.user_recommend,user.group,user.country,user.user_phone,position.user_position')
            ->join('position ON user.position = position.id','left')->where('user.user_id > 1000')->select();
        $csv=new \Think\Csv();
        $csv_title=array('MT4','姓名','代理人','组别','国家','电话','职位');
        $csv->put_csv($users,$csv_title);
    }

}